package com.qire.manhua.model.viewModel.impl;

import android.view.View;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.support.AbsBringDialogViewModel;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.dal.service.ComicQuickReadService;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.viewModel.IComicQuickReadViewModel;

public class ComicQuickReadViewModel extends AbsBringDialogViewModel<IComicQuickReadViewModel> {

    private ComicQuickReadService quickReadService = new ComicQuickReadService(this);

    private ComicBookEntity comicBookEntity;

    private int nextChaptersId;

    private int historyChaptersId;

    public ComicQuickReadViewModel() {
        put("pageNo", 1);
        hideHistory();
    }


    //region    #附加管理 begin
    //endregion #附加管理 end


    //region    #数据操作 begin
    public ComicBookEntity comicBookEntity() {
        return comicBookEntity;
    }

    public ComicBookEntity readHistory() {
        comicBookEntity.chaptersReading(historyChaptersId);
        return comicBookEntity;
    }

    public ComicBookEntity readNext() {
        comicBookEntity.chaptersReading(nextChaptersId);
        return comicBookEntity;
    }

    /**
     * 加载漫画快速阅读信息
     * @param comicBookEntity 漫画实体
     */
    public void loadComicBookEntity(ComicBookEntity comicBookEntity) {
        this.comicBookEntity = comicBookEntity;
        quickReadService.fetchComicQuickReadInfo(comicBookEntity.id);

        refreshFavoritesBtnUI();
    }

    public void setNextChaptersId(int nextChaptersId) {
        this.nextChaptersId = nextChaptersId;
    }

    /**
     * 添加或移除收藏夹
     */
    public void whetherToFavorites() {
        if(ObjectUtil.nonNull(comicBookEntity)) {
            quickReadService.toggleCollectionBy(comicBookEntity.id);
        }
    }

    /**
     * 刷新收藏结果
     * @param resultMsg 收藏结果信息
     * @param takeMsg 任务信息
     */
    public void refreshFavorite(String resultMsg, String takeMsg) {
        if(StringUtil.notContains(resultMsg, "失败")) {
            comicBookEntity.whetherCollect(StringUtil.notContains(resultMsg, "取消"));
        }

        refreshFavoritesBtnUI();

        ToastHelper.showSnackBar(resultMsg, Prompt.SUCCESS);

        if(StringUtil.notEmpty(takeMsg)) {
            ToastHelper.showSnackBar(takeMsg, Prompt.SUCCESS);
        }
    }
    //endregion #数据操作 end

    //region    #UI操作 begin
    public void showHistory() {
        put("historyVisible", View.VISIBLE);
    }

    public void hideHistory() {
        put("historyVisible", View.GONE);
    }

    public void loadHistoryInfo(int historyChaptersId, int historyChaptersPx) {
        this.historyChaptersId = historyChaptersId;

        put("historyRecord", String.format("[第%s话]", historyChaptersPx));
        if(historyChaptersPx > 1) {
            showHistory();
        } else {
            hideHistory();
        }
    }

    public void refreshChapterInfo(CharSequence chaptersTitle) {
        put("pictureResourceList", comicBookEntity.currentReadingChapters().pictureResourcesList);
        put("chaptersTitle", chaptersTitle);
        put("pageNo", 0);
    }

    /**
     * 刷新收藏按钮UI显示
     */
    public void refreshFavoritesBtnUI() {
        boolean isFavorite = ObjectUtil.safeRun(comicBookEntity, comic -> comic.whetherCollect(), false);
        int collectBtnBgRid = isFavorite ? R.drawable.app_quick_read_button_gray : R.drawable.app_quick_read_button_yellow;
        int collectBtnIconRid = isFavorite ? R.drawable.app_quick_read_icon_collected : R.drawable.app_quick_read_icon_collection;
        put("collectBtnBgRid", collectBtnBgRid);
        put("collectBtnIconRid", collectBtnIconRid);
    }
    //endregion #UI操作 end

}
